<?php
namespace app\teacher\controller;

use think\Db;
//课后练习
class Papers extends Base {

	//一级分类
	public function cate_lists (){
		$school_id = request()->get('school_id');
		$lists = Db::name('exam_cate')->where(['parent_id'=>0,'uid'=>$school_id])->field('id,name')->select();
		return messages(200, '请求成功', $lists);
	}

	//下级列表
	public function xiaji_cate(){
		$cate_id = request()->get('cate_id');
		$lists = Db::name('exam_cate')->where(['parent_id'=>$cate_id])->field('id, name')->select();
		return messages(200, '请求成功', $lists);
	}

	//获取题
	public function get_question(){
		$cate_id = request()->get('cate_id');
		$data = Db::name('exam_question')->where(['cate_id'=>$cate_id])->select();
		return messages(200, '请求成功', $data);
	}

	//发布
	public function publish(){
		$param = request()->post();
        $times = time();
		if(isset_full($param,'quesion_ids')){
			$quesion = implode(',',$param['quesion_ids']);
			$res['question_ids'] = $quesion;
            $res1['question_ids'] = $quesion;
		}else{
			return messages(201, '请选择试题');
		}
		if(isset_full($param,'classes_id')){
			$res['classes_ids'] = $param['classes_id'];
            $res1['classes_id'] = $param['classes_id'];
		}else{
			return messages(201, '请选择班级');
		}
		if(isset_full($param,'answer_time')){
            $res1['answer_time'] = $param['answer_time'];
			$res2['ans_time'] = $param['answer_time'];
		}else{
			return messages(201, '请输入答题时间');
		}
		if(isset_full($param,'title')){
			$res['title'] = $param['title'];
            $res1['title'] = $param['title'];
		}else{
			return messages(201, '请输入标题');
		}
		if(isset_full($param,'school_id')){
			$res['uid'] = $param['school_id'];
            $res1['uid'] = $param['school_id'];
		}else{
			return messages(201, '参数错误');
		}
		$res['share_ids'] = 0;
		$res['profile_id'] = $this->teacher_id();
        $res1['profile_id'] = $this->teacher_id();
		$res['createtime'] = $times;
        $res1['createtime'] = $times;
        try {
            // 开启事务
            Db::startTrans();
            //添加题
            $rs = Db::name('exam_papers')->insertGetId($res);
            $res1['papers_id'] = $rs;
            $res1['num'] = 1;
            $res1['type'] = 2;
            //添加题关联
            $rs1 = Db::name('exam_papers_gl')->insertGetId($res1);
            $user = Db::name('classes_student')->where(['classes_id'=>$param['classes_id']])->field('uid')->select();
            if($user){
                $res2['papers_id'] = $rs;
                $res2['papers_gl_id'] = $rs1;
                $res2['start_time'] = $times;
                $res2['num'] = 1;
                $res2['class_id'] = $param['classes_id'];
                foreach ($user as $key => $value){
                    $res2['user_id'] = $value['uid'];
                    Db::name('exam_user_papers')->insert($res2);
                }
            }
            // 提交事务
            Db::commit();
            return messages(200, '发布成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            // 处理异常，比如记录日志、返回错误信息等
            return messages(201, '发布失败');
        }

	}

	//列表
	public function lists(){
        $uid = $this->teacher_id();
        $map['profile_id'] = $uid;
		$lists = Db::name('exam_papers_gl')->where($map)->field('id,title,createtime')->order('createtime desc')->select();
        if($lists){
            foreach ($lists as $key => $value){
                $lists[$key]['createtime'] = date('Y-m-d',$value['createtime']);
            }
        }
        return messages(200, '请求成功', $lists);
	}

	//预览
	public function preview(){
		$papers_id = request()->get('papers_id');
		$quesion_ids = Db::name('exam_papers_gl')->where(['id'=>$papers_id])->value('question_ids');
		$res = [];
        if($quesion_ids){
            $ids = explode(',',$quesion_ids);
            foreach ($ids as $key => $value) {
                $quesion = Db::name('exam_question')->where(['id'=>$value])->find();
                $res[$key]['id'] = $quesion['id'];
                $res[$key]['kind'] = $quesion['kind'];
                $res[$key]['answer'] = $quesion['answer'];
                $res[$key]['describe'] = $quesion['describe'];
                $res[$key]['difficulty'] = $quesion['difficulty'];
                $res[$key]['options_json'] = json_decode($quesion['options_json']);
                $res[$key]['other_name'] = $quesion['other_name'];
                $res[$key]['other_url'] = $quesion['other_url'];
                $res[$key]['title'] = $quesion['title'];

            }
        }
		return messages(200, '请求成功', $res);
	}

	//作业概述
	public function overview(){
		$papers_id = request()->get('papers_id');
		$data = Db::name('exam_papers_gl')->alias('a')
			->join('fa_classes_class b','a.classes_id=b.id')
			->field('a.id,a.title,b.title as class_title,a.profile_id,a.question_ids,a.classes_id,a.start_time,a.end_time,a.end_time,a.num,b.title as class_name')
			->where(['a.id'=>$papers_id])
			->find();
        if($data){
            $question_ids = count(explode(',',$data['question_ids']));
            $data['start_time'] = $data['start_time'] ? date('Y-m-d H:i:s',$data['start_time']) : '';
            $data['end_time'] = $data['end_time'] ? date('Y-m-d H:i:s',$data['end_time']) : '';
            //学生人数
            $data['class_student'] = Db::name('classes_student')->where(['classes_id'=>$data['classes_id']])->count();
            //答题人数数据
            $dati = Db::name('exam_user_achievement')->where(['user_papers_id'=>$data['id']])->group('user_id')->select();
            //答题人数
            $data['answer_num'] = count($dati);
            //没有答题的人数
            $user_badge = Db::name('user_badge')->alias('a')
                ->join('fa_classes_student b','a.user_id=b.uid')
                ->field('user_id,b.realname')
                ->where(['a.classes_id'=>$data['classes_id'],'papers_id'=>$data['id']])
                ->select();
            $data['user_badge'] = $user_badge;//获取徽章的学生名称
            //未完成学生人名
            $no_student_name = Db::name('exam_user_papers')->alias('a')
                ->join('fa_classes_student b','a.user_id=b.uid')
                ->field('a.user_id,b.realname')
                ->where(['a.class_id'=>$data['classes_id'],'a.papers_gl_id'=>$data['id'],'a.status'=>1])
                ->select();
            $data['no_student_name'] = $no_student_name;
            //允许提交次数百分比
            $i = 0;
            if($dati){
                foreach ($dati as $dak => $dav) {
                    $dacount = Db::name('exam_user_achievement')->where(['user_id' => $dav['user_id'], 'user_papers_id' => $data['id']])->count();
                    if ($dacount == $data['num']) {
                        $i++;
                    }
                }
            }
            //提交次数百分比
            $data['tjcs_bfb'] = round(($i / $data['class_student']) *  100);
            //首次提交正确率
            $sc_bfb = Db::name('exam_user_achievement')->where(['user_papers_id'=>$data['id']])->group('user_id')->field('score')->select();
            $sc_zql = 0;
            if($sc_bfb){
                foreach ($sc_bfb as $sck => $scv){
                    $sc_zql += round($scv['score'] / $question_ids * 100);
                }
                $sc_zql = round($sc_zql / $data['class_student']);
            }
            $data['sc_zql'] = $sc_zql;
            //平均分
            $pingjunfen = 0;
            if($dati){
                foreach ($dati as $dak => $dav) {
                    $dadata = Db::name('exam_user_achievement')->where(['user_id' => $dav['user_id'], 'user_papers_id' => $data['id']])->field('score')->select();
                    foreach ($dadata as $value){
                        $pingjunfen += round(($value['score'] / $question_ids) * 100);
                    }
                }
                $pingjunfen = round($pingjunfen / $data['class_student']);
            }
            $data['pjf'] = $pingjunfen;
        }
		return messages(200, '请求成功', $data);
	}

    //成绩单
    public function report_card(){
        $papers_id = request()->get('papers_id');
        $data = Db::name('exam_papers_gl')->where(['id'=>$papers_id])->find();
        $student = Db::name('classes_student')->alias('a')
            ->join('fa_classes_class b','a.classes_id=b.id')
            ->field('a.student_number,a.uid,a.realname,b.title')
            ->order('a.student_number asc')
            ->where(['a.classes_id'=>$data['classes_id']])->select();
        if($student){
            foreach ($student as $key => $value){
                $achievement = Db::name('exam_user_achievement')->where(['user_id'=>$value['uid'],'user_papers_id'=>$data['id']])->find();
                $student[$key]['state'] = 2;
                $student[$key]['score_lv'] = '';
                if($achievement){
                    $student[$key]['state'] = 1;
                    $student[$key]['score_lv'] = round($achievement['score'] / ($achievement['score'] + $achievement['wrong']) * 100);
                }
            }
        }
        return messages(200, '请求成功', $student);
    }

	//成绩单
//	public function report_card(){
//		$papers_id = request()->get('papers_id');
//        $lists = Db::name('exam_user_achievement')->alias('a')
//            ->join('fa_classes_student b','a.user_id=b.uid')
//            ->field('b.realname,b.student_number,b.classes_id,a.score,a.wrong,a.grade')
//            ->where(['a.user_papers_id'=>$papers_id])
//            ->select();
//        if($lists){
//            foreach ($lists as $key => $value){
//                $lists[$key]['state'] = 1;
//                $lists[$key]['title'] = Db::name('classes_class')->where(['id'=>$value['classes_id']])->value('title');
//                $lists[$key]['score_lv'] = round($value['score'] / ($value['score'] + $value['wrong']) * 100);
//            }
//        }
//        return messages(200, '请求成功', $lists);
//	}

}